package com.qinggeng.project.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.project.system.domain.OrdersGift;
import com.qinggeng.project.system.domain.OrdersGoods;
import com.qinggeng.project.system.domain.SumPointSale;
import com.qinggeng.project.system.domain.vo.SumPointSaleVo;
import com.qinggeng.project.system.mapper.SumPointSaleMapper;
import com.qinggeng.project.system.service.SumPointSaleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * @author 吴鹏飞
 * @date 2020/11/17
 * 配送区域总自提点销量统计业务实现
 */
@Slf4j
@Service
public class SumPointSaleServiceImpl implements SumPointSaleService {
    @Resource
    private SumPointSaleMapper sumPointSaleMapper;

    @Override
    public SumPointSale getSumPointSaleById(Long id) {
        return sumPointSaleMapper.selectSumPointSaleById(id);
    }

    @Override
    public SumPointSale getSumPointSaleByAreaNameAndGoodsIdAndRecordDate(String areaName, Long goodsId, String recordDate) {
        return sumPointSaleMapper.getSumPointSaleByAreaNameAndGoodsIdAndRecordDate(areaName,goodsId,recordDate);
    }

    public SumPointSale getSumPointSaleByAreaNameAndGiftIdAndRecordDate(String areaName,Long giftId,String recordDate){
        return sumPointSaleMapper.getSumPointSaleByAreaNameAndGiftIdAndRecordDate(areaName,giftId,recordDate);
    }

    @Override
    public boolean addSumPointSaleNum(Long id, Integer saleNum) {
        SumPointSale sumPointSale=new SumPointSale();
        sumPointSale.setId(id);
        sumPointSale.setSaleNum(saleNum);
        sumPointSale.setUpdateTime(DateUtils.getNowDate());
        return sumPointSaleMapper.addSumPointSaleNum(sumPointSale)>0;
    }

    @Override
    public boolean subtractSumPointSaleNum(Long id, Integer saleNum) {
        SumPointSale sumPointSale=new SumPointSale();
        sumPointSale.setId(id);
        sumPointSale.setSaleNum(saleNum);
        sumPointSale.setUpdateTime(DateUtils.getNowDate());
        return sumPointSaleMapper.subtractSumPointSaleNum(sumPointSale)>0;
    }


    @Override
    public List<SumPointSale> listSumPointSale(SumPointSale sumPointSale) {
        return sumPointSaleMapper.selectSumPointSaleList(sumPointSale);
    }

    @Override
    public int insertSumPointSale(SumPointSale sumPointSale) {
        Date nowDate= DateUtils.getNowDate();
        sumPointSale.setCreateTime(nowDate);
        sumPointSale.setGoodsTime(nowDate);
        sumPointSale.setPrint("0");//0=未打印
        return sumPointSaleMapper.insertSumPointSale(sumPointSale);
    }

    @Override
    public void updateSumPointSale(String sortArea, OrdersGoods ordersGoods,String recordDate){
        SumPointSale sumPointSale=this.getSumPointSaleByAreaNameAndGoodsIdAndRecordDate(sortArea,ordersGoods.getGoodsId(),recordDate);
        if(sumPointSale!=null){
            boolean result=this.addSumPointSaleNum(sumPointSale.getId(),ordersGoods.getGoodsNum());
            if(!result){
                log.error("新增区域销量失败");
                throw new CustomException("新增区域销量失败");
            }
            return;
        }
        sumPointSale=new SumPointSale();
        sumPointSale.setSortArea(sortArea);
        sumPointSale.setGoodsId(ordersGoods.getGoodsId());
        sumPointSale.setGoodsName(ordersGoods.getGoodsName());
        sumPointSale.setAttributes(ordersGoods.getAttributes());
        sumPointSale.setSaleNum(ordersGoods.getGoodsNum());
        sumPointSale.setRecordDate(recordDate);
        //10=商品
        sumPointSale.setType(10);
        int rows=this.insertSumPointSale(sumPointSale);
        if(rows<1){
            throw new CustomException("新增区域销量失败");
        }
    }


    @Override
    public void updateSumPointSale(String areaName, OrdersGift ordersGift, String recordDate){
        SumPointSale sumPointSale=this.getSumPointSaleByAreaNameAndGiftIdAndRecordDate(areaName,ordersGift.getGiftId(),recordDate);
        if(sumPointSale!=null){
            boolean result=this.addSumPointSaleNum(sumPointSale.getId(),ordersGift.getGiftNum().intValue());
            if(!result){
                log.error("新增区域销量失败");
                throw new CustomException("新增区域销量失败");
            }
            return;
        }
        sumPointSale=new SumPointSale();
        sumPointSale.setSortArea(areaName);
        sumPointSale.setGoodsId(ordersGift.getGiftId());
        sumPointSale.setGoodsName(ordersGift.getGoodsName());
        sumPointSale.setAttributes(ordersGift.getGoodsName());
        sumPointSale.setSaleNum(ordersGift.getGiftNum().intValue());
        sumPointSale.setRecordDate(recordDate);
        //20=奖品
        sumPointSale.setType(20);
        int rows=this.insertSumPointSale(sumPointSale);
        if(rows<1){
            log.error("新增区域销量失败");
            throw new CustomException("新增区域销量失败");
        }
    }

    @Override
    public int updateSumPointSale(SumPointSale sumPointSale) {
        return sumPointSaleMapper.updateSumPointSale(sumPointSale);
    }

    @Override
    public int deleteSumPointSaleById(Long id) {
        return sumPointSaleMapper.deleteSumPointSaleById(id);
    }

    @Override
    public int deleteSumPointSaleByIds(Long[] ids) {
        return sumPointSaleMapper.deleteSumPointSaleByIds(ids);
    }

    /**
     * 查询区域自提点销量
     * @return
     */
    @Override
    public List<SumPointSaleVo> listSumPointSaleByYesterday(String sortArea,String recordDate) {
        PageHelper.startPage(1,20);
        return sumPointSaleMapper.listSumPointSaleByYesterday(sortArea,recordDate);
    }
    /**
     * 打印区域
     * @param sortArea
     */
    @Override
    public void printSumPointSaleList(String sortArea) {
        sumPointSaleMapper.printSumPointSaleList(sortArea);
    }

    @Override
    public SumPointSale selectSumlePointSaleRecordById(Long id) {
        SumPointSale sumPointSale = sumPointSaleMapper.selectByPrimaryKey(id);
        return sumPointSale;
    }

    @Override
    public boolean updatePrintStatusById(String id) {
        SumPointSale sumPointSale=new SumPointSale();
        sumPointSale.setId(Long.valueOf(id));
        sumPointSale.setUpdateTime(new Date());
        sumPointSale.setPrint("1");
        boolean bl=sumPointSaleMapper.updateByPrimaryKeySelective(sumPointSale)>0;
        if (bl==false){
            throw  new CustomException(id+"打印状态没有修改成功");
        }
        return true;
    }
}
